package com.Artificialintelligence.web.controller.system;

import cn.hutool.jwt.JWTUtil;
import com.Artificialintelligence.common.annotation.Log;
import com.Artificialintelligence.common.core.domain.AjaxResult;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 识别算法接口
 *
 * 陈家林
 * */
@RestController
@RequestMapping("/system/ocr")
public class ThirdpartyOcrController {

    private JWTUtil jwtUtil;

    private String SecretId = "AKIDe9OM4WhbFdjY7PP8amiNltkKdgRv84k9"; //腾讯云AK
    private String SecretKey = "91dy2pb7LIyOWNxHK6astd6IEuIPGq9C"; //腾讯云SK

    /**
     * 腾讯云--身份证识别
     */
    @Log(title = "身份证识别")
    // /authRole/{userId}
    @PostMapping("/IdCardRequestBody")
    public AjaxResult IdCardRequestBody(@Validated @RequestBody String file){
        try{
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(SecretId, SecretKey);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            OcrClient client = new OcrClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            IDCardOCRRequest req = new IDCardOCRRequest();
            req.setImageUrl(file);
            // 返回的resp是一个IDCardOCRResponse的实例，与请求对象对应
            IDCardOCRResponse resp = client.IDCardOCR(req);
            // 输出json格式的字符串回包
//            System.out.println(IDCardOCRResponse.toJsonString(resp));
            return AjaxResult.success(resp);
        } catch (TencentCloudSDKException e) {
//            System.out.println(e.toString());
            return AjaxResult.error("识别错误,请上传正确的图片");
        }
    }

    /**
     * 腾讯云--车牌识别
     */
    @Log(title = "车牌识别")
    @PostMapping("/RecognizeLicense")
    public AjaxResult RecognizeLicense(@Validated @RequestBody String file){
        try{
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(SecretId, SecretKey);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            OcrClient client = new OcrClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            LicensePlateOCRRequest req = new LicensePlateOCRRequest();
            req.setImageUrl(file);
            // 返回的resp是一个LicensePlateOCRResponse的实例，与请求对象对应
            LicensePlateOCRResponse resp = client.LicensePlateOCR(req);
            // 输出json格式的字符串回包
            return AjaxResult.success(resp);
        } catch (TencentCloudSDKException e) {
           return AjaxResult.error("识别错误,请上传正确的图片");
        }
    }

}
